<?php

namespace App\Models;

use App\Enums\DomainType;
use App\Models\Traits\BelongsToApplication;
use App\Models\Traits\HasEasyStatus;
use App\Models\Traits\HasLikes;
use EasyWeChat\OfficialAccount\Application;
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use InvalidArgumentException;

class Chapter extends Model
{
    use BelongsToApplication,
        Cachable,
        HasEasyStatus,
        HasLikes;

    protected $casts = [
        'start_at' => 'datetime',
        'end_at' => 'datetime',
    ];

    public function wechat(): BelongsTo
    {
        return $this->belongsTo(Wechat::class);
    }

    public function season(): BelongsTo
    {
        return $this->belongsTo(Season::class);
    }

    public function sponsors(): HasMany
    {
        return $this->hasMany(Sponsor::class);
    }

    public function players(): HasMany
    {
        return $this->hasMany(Player::class);
    }

    /**
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     */
    public function initWechat(): Application
    {
        if ($this->wechat && $this->wechat->isEnabled()) {
            $config = [
                'app_id' => $this->wechat->appid,
                'secret' => $this->wechat->secret,
            ];
        } elseif ($this->season->wechat && $this->season->wechat->isEnabled()) {
            $config = [
                'app_id' => $this->season->wechat->appid,
                'secret' => $this->season->wechat->secret,
            ];
        } else {
            throw new InvalidArgumentException('微信公众号配置错误');
        }

        return new Application($config);
    }

    public function getMiddleDomain(): ?DomainPool
    {
        if ($this->spider()->where('type', DomainType::MIDDLE)->exists()) {
            return $this->spider()->where('type', DomainType::MIDDLE)->first()->domainPool;
        } else {
            return $this->season->getMiddleDomain();
        }
    }

    public function spider(): MorphOne
    {
        return $this->morphOne(Spider::class, 'spiderable');
    }

    public function getPlayerDomain(): ?DomainPool
    {
        if ($this->spider()->where('type', DomainType::PLAYER)->exists()) {
            return $this->spider()->where('type', DomainType::PLAYER)->first()->domainPool;
        } else {
            return $this->season->getPlayerDomain();
        }
    }

    public function getRankDomain(): ?DomainPool
    {
        if ($this->spider()->where('type', DomainType::RANK)->exists()) {
            return $this->spider()->where('type', DomainType::RANK)->first()->domainPool;
        } else {
            return $this->season->getRankDomain();
        }
    }

    public function getType(): string
    {
        return '<span class="label" style="background:#21b978">场次</span>';
    }

    public function getTitle()
    {
        return $this->title;
    }
}
